package com.graduation.fq.student.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.graduation.fq.student.Args.AlterLoginEventArgs;
import com.graduation.fq.student.Args.EventArgs;
import com.graduation.fq.student.Logic.LoginAndAlterPwdLogic;
import com.graduation.fq.student.R;
import com.graduation.fq.student.Utils.CommonUtils;
import com.graduation.fq.student.interfaces.EventListener;

public class AlterPwdActivity extends Activity {

    private EditText idEdit;
    private EditText oldPwdEdit;
    private EditText newPwdEdit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_pwd);
        idEdit = (EditText) findViewById(R.id.alter_u_id);
        oldPwdEdit = (EditText) findViewById(R.id.alter_old_pwd);
        newPwdEdit = (EditText) findViewById(R.id.alter_new_pwd);

        findViewById(R.id.alter_pwd_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alterPwd();
            }
        });
    }

    /**
     * 修改密码
     */
    private void alterPwd() {

        String id = CommonUtils.getString(idEdit);
        String oldPwd = CommonUtils.getString(oldPwdEdit);
        String newPwd = CommonUtils.getString(newPwdEdit);

        if (CommonUtils.isNotNullOrEmpty(id, oldPwd, newPwd)){
            LoginAndAlterPwdLogic.getInstance().alterPwd(id, oldPwd, newPwd, new EventListener() {
                @Override
                public void onEvent(EventArgs args) {
                    AlterLoginEventArgs result = (AlterLoginEventArgs)args;
                    if (result.isLogin()) {
                        CommonUtils.Toast(AlterPwdActivity.this, "改密成功, 请重新登录");
                        startActivity(new Intent(AlterPwdActivity.this, LoginActivity.class));
                        finish();
                    }else{
                        CommonUtils.Toast(AlterPwdActivity.this, "改密失败, 请重试");
                    }
                }
            });
        }
    }
}
